#ifndef __BHTTP_THREAD_H__
#define __BHTTP_THREAD_H__
#pragma once

#include <string>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <atomic>
#include <functional>
#include <condition_variable>

namespace bhttp
{
class thread
{
    using task = std::function<void()>;
public:
    thread(const thread&) = delete;
    thread& operator=(const thread&) = delete;

    explicit thread(const std::string& name = std::string("bhttp::thread"));
    ~thread();

    void start();
    void stop();
    void run(const task& tk);
    void run(task&& tk);

private:
    void thread_func();
    bool is_full() const;
    task take();

private:
    std::string name_;
    std::atomic<bool> running_{ false };
    std::unique_ptr<std::thread> thread_;
    std::deque<task> queue_;
    std::mutex mutex_;
    std::condition_variable not_full_;
    std::condition_variable not_empty_;
    size_t max_queue_size_{ 0 };
};
}

#endif //__BHTTP_THREAD_H__
